{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow import keras\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cifar = keras.datasets.cifar10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_image,train_label),(test_image,test_label) = cifar.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x21c806ab4a8>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEAAAAA+CAYAAACbQR1vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAACixJREFUaIHtmltsHNd5x3/fOTOzN3JJihSpm21ZiGMjSB3blV0gSgv3\nIYXrxEjyliBIHhIgfWmQAMlD0b4EyGuTPgZwEL8ERoMALYrEUO22aGTYCdxKdlxdLFGiriRFUlyR\n3F3uZS7nfH3YNaOqtrmiqF0H4R+YxcyZOd/5z3++c/bMf46oKn/IMIMmMGjsCDBoAoPGjgCDJjBo\n7AhwN5VF5BkRmRaRGRH5m+0i1U/IVucBImKB88CngTngOPAlVX1n++jde9xNBjwFzKjqJVVNgJ8B\nn9seWv3D3QiwH5i95XiuW/Z7heBeNyAi3wC+AVAqlf74kUceueMYLk1J4jbOOwrFEjYIAPnAOleu\nXKFSqXzwRdydAPPAfbccH+iW/R+o6vPA8wCHDx/W48eP99yAqmJEOfvmG5w49h+krSaf+fLXmDr4\nIB6LinnfFH7yySd7auNuusBx4CEReVBEIuCLwC82qyQiPW0AxniWFmb5zWuvUa+uE5qAX7/8EisL\nC6CKen3fmL1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BIEIWEu\nAAEJwFqLV3AqOOewgcWEhigIUVWcc2x8wFHD6OgoaZoSJwlOFBFBtfPegEsBxbnenKFNBZCOtD8B\nzqrqD28p39sdHwC+AJzeLFYul+OBgwdBDctrq2QuJT+UxzmHcx7nIfMe7xyZpmiagX/XG1C882Qp\nZFlGmqZ4FLoZ4L2nkM+RCyxGpNcxsKcMOAJ8BTglIm93y/4W+JKIPEanC1wB/qqXBkUgSWPSNMFY\nS2AM6jxJlhFnDjHSHfAMPutYI52hD1QEFYcJhNB2ZnoqdLOk4ykaMaB+w1m6awFU9XXe23u9QzMU\nUIjjzs0ncUySZnjt/M1Za8nncpjAkqUp3nvEWATBGEPUndmtr69jTWemp6rEcUyz2UJEsLmILIkx\nYrZ/ENwOqCppmpBlGUEQgLEIbDxxNUKaZRhjEBRrQ4yxG31cvSeKItrtNlmWEYbhRl3nHMYIxVyx\n6zn3JsCWvw5vBSJSB6bvQegJoHJb2QOqunuziv32BKdV9fB2BxWRE1uNu7NCZNAEBo1+C/D8hy1u\nXwfBDyN2ukC/GtqOFWXbac5sQN/9MnsPNzrrBy4Ch4AI+B/gY1uIsxd4ors/TGeV2seA7wHf3Qq3\nfmXAtqwoU9UFVX2ru18HtmzOvIt+CbDtK8puM2egY86cFJEXRGSs1zi/l4Pg7eYM8CM63esxOvbd\nD3qN1S8BelpR1gvez5xRVaedd+Af0+lyPaFfAmxpRdnt+CBz5pbLejJnNtCPf4HuqP0snVH7IvB3\nW4zxKToGzEng7e72LPBT4FS3/BfA3l5j7swEB01g0NgRYNAEBo0dAQZNYNDYEWDQBAaNP3gB/hcl\nmQriGMwSSQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x21c80606a90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(0.5,0.5))\n",
    "plt.imshow(train_image[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "神经网络：最好做归一化处理。对图片的归一化:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((50000, 32, 32, 3), (10000, 32, 32, 3))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_image.shape,test_image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_image = train_image/255 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test_image = test_image/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = keras.Sequential()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.add(keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(keras.layers.Conv2D(32,(3,3),activation='relu'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.add(keras.layers.MaxPool2D())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.add(keras.layers.Flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(keras.layers.Dense(64,activation='relu'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.add(keras.layers.Dropout(0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.add(keras.layers.Dense(10,activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d (Conv2D)              (None, 26, 26, 32)        320       \n",
      "_________________________________________________________________\n",
      "conv2d_1 (Conv2D)            (None, 24, 24, 32)        9248      \n",
      "_________________________________________________________________\n",
      "max_pooling2d (MaxPooling2D) (None, 12, 12, 32)        0         \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (None, 4608)              0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 64)                294976    \n",
      "_________________________________________________________________\n",
      "dropout (Dropout)            (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 305,194\n",
      "Trainable params: 305,194\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['acc'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "history = model.fit(train_image,train_label,epochs=20,batch_size=256,validation_data=(test_image,test_label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:kr]",
   "language": "python",
   "name": "conda-env-kr-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
